package com.Utils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
@Service
@Slf4j
public class TimestampUtil {
    public String getResult(long timeStamp1,long timeStamp2){//2:现在的时间，1：评论的时间
        Instant instant = Instant.ofEpochMilli(timeStamp1);
        Instant instant1 = Instant.ofEpochMilli(timeStamp2);
        LocalDateTime timestampDateTime = LocalDateTime.ofInstant(instant, ZoneId.systemDefault());
        LocalDateTime currentDateTime = LocalDateTime.ofInstant(instant1, ZoneId.systemDefault());
        Duration duration = Duration.between(timestampDateTime,currentDateTime);//现在的时间与评论时间对比
        String result = "";
        System.out.println(duration.toDays());
        if (duration.toDays() >= 1) {
            // 大于一天，返回年月日表达式
            result = timestampDateTime.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
        } else if (duration.toHours() >= 1) {
            // 在一天内且大于一个小时，返回小时差
            result = String.valueOf(duration.toHours()) + "小时前";
        } else if(duration.toMinutes()>1&&duration.toMinutes()<=59){
            // 在一小时内，返回分钟数
            result = String.valueOf(duration.toMinutes()) + "分钟前";
        }else{
            result="刚刚";
        }
        System.out.println(result);
        return result;
    }

}
